<?php
if(!defined("Hosticali"))
	die();
class Core
{	
	/**** Database Session Variables ***/
	public var $result;
	public var $sess = array();
	public var $currentSess;
	public var $handle = array();
	
	public function __construct()
	{
		global $url;
		$settings = getSettings();
		$sess['default'] = array(
			'host' => $host,
			'user' => $user,
			'pass' => $pass,
			'dbse' => $dbse,
			'pfix' => $pfix
		);
		$this->currentSess = 'default';
		$this->handle['default'] = mysql_connect(
			$this->sess[$this->currentSess]['host'],
			$this->sess[$this->currentSess]['user'],
			$this->sess[$this->currentSess]['pass']
		);
		if(!$this->checkErr('handle', 'default'))
			return false;
		mysql_select_db(
			$this->sess[$this->currentSess]['dbse'],
			$this->handle[$this->currentSess]
		);
		if(!$this->checkErr('handle', 'default'))
			return false;
		else
			return true;
	}
	public function checkErr($type, $id = false)
	{
		if($type == "result")
		{
			if(!$this->result)
				return false;
			else
				return true;
		}
		else
		{
			if(!$this->handle[$id])
				return false;
			else
				return true;
		}
	}
	public function switchSess($identifier)
	{
		if(!$this->handle[$identifier])
			return false;
		$this->currentSess = $identifier;
		$this->result = false;
		$this->handle[$identifier] = mysql_connect(
			$this->sess[$this->currentSess]['host'],
			$this->sess[$this->currentSess]['user'],
			$this->sess[$this->currentSess]['pass']
		);
		if(!$this->checkErr('handle', $identifier))
			return false;
		mysql_select_db(
			$this->sess[$this->currentSess]['dbse'],
			$this->handle
		);
		if(!$this->checkErr('handle', $identifier))
			return false;
		else
			return true;
	}
	public function addSess($identifier, $host, $user, $pass, $dbse, $pfix)
	{
		$this->sess[$identifier] = array(
			'host' => $host,
			'user' => $user,
			'pass' => $pass,
			'dbse' => $dbse,
			'pfix' => $pfix
		);
		return true;
	}
	public function destroySess($identifier)
	{
		mysql_close($this->handle[$identifier]);
		unset($this->handle[$identifier], $this->sess[$identifier]);
		return true;
	}
	public function Query($sql)
	{
		$this->result = mysql_query(str_ireplace("<PRE>", $this->sess[$this->currentSess]['pfix'], $sql), $this->handle[$currentSess]);
		if(!$this->checkErr('result'))
			return false;
		else
			return true;
	}
	public function FreeResult()
	{
		$oldResult = $this->result;
		mysql_free_result($this->result, $this->handle[$currentSess]);
		if(!$this->checkErr('result'))
		{
			$this->result = $oldResult;
			return false;
		}
		else
			return true;
	}
	public function Clean($sql)
	{
		return mysql_real_escape_string($sql, $this->handle[$currentSess]);
	}
	public function GetArray()
	{
		return mysql_fetch_assoc($this->result);
	}
	public function NumRows()
	{
		return mysql_num_rows($this->result);
	}
	public function Affected()
	{
		return mysql_affected_rows($this->handle[$currentSess]);
	}
}
?>